﻿using System;

namespace TSharp.Core.Asm
{
    /// <summary>
    /// 
    /// </summary>
    public enum Priority : byte
    {
        /// <summary>
        /// 
        /// </summary>
        Lowest = 0,
        /// <summary>
        /// 
        /// </summary>
        Low = 63,
        /// <summary>
        /// 
        /// </summary>
        Normal = 127,
        /// <summary>
        /// 
        /// </summary>
        High = 191,
        /// <summary>
        /// 
        /// </summary>
        Highest = 255,
    }

    /// <summary>
    /// 注册容器的层级
    /// </summary>
    public enum Level : byte
    {
        /// <summary>
        /// 
        /// </summary>
        ROOT = 1 << 0,
        /// <summary>
        /// 
        /// </summary>
        SESSION = 1 << 1,
        /// <summary>
        /// 
        /// </summary>
        REQUSET = 1 << 2,
    }

    /// <summary>
    /// 服务注册定义
    /// <para>2011/2/25</para>
    /// 	<para>THINKPADT61</para>
    /// 	<para>tangjingbo</para>
    /// </summary>
    public class ServiceAttribute : ExtensionAttribute
    {
        private Level _ContainerLevel = Level.REQUSET;

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceAttribute"/> class.
        /// </summary>
        /// <param name="serviceType">服务类类型</param>
        public ServiceAttribute(Type serviceType)
            : this(null, serviceType)
        {
        }

        /// <summary>
        /// 注册一个服务
        /// </summary>
        /// <param name="serviceType">服务类型</param>
        /// <param name="level">容器级别.</param>
        public ServiceAttribute(Type serviceType, Level level)
            : this(null, serviceType)
        {
            this.Level = level;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceAttribute"/> class.
        /// </summary>
        /// <param name="intfType">服务接口类型</param>
        /// <param name="implType">服务实现类型</param>
        public ServiceAttribute(Type intfType, Type implType)
        {
            IntfType = intfType;
            ImplType = implType;
            Priority = Priority.Normal;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceAttribute"/> class.
        /// </summary>
        /// <param name="intfType">服务接口类型</param>
        /// <param name="implType">服务实现类型</param>
        /// <param name="priority">注册优先级.</param>
        public ServiceAttribute(Type intfType, Type implType, Priority priority)
        {
            IntfType = intfType;
            ImplType = implType;
            Priority = priority;
        }

        /// <summary>
        /// 注册到UnityContainer中的Name，可为null
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }

        /// <summary>
        /// 接口类型或抽象类
        /// </summary>
        public Type IntfType { get; private set; }

        /// <summary>
        /// 实现intfType的最终类型，必须有一个默认的无参构造类
        /// </summary>
        public Type ImplType { get; private set; }

        /// <summary>
        /// 优先级，总是使用优先级最高的实现类
        /// </summary>
        public Priority Priority { get; set; }

        /// <summary>
        /// 服务驻留级别
        /// </summary>
        /// <value>The level.</value>
        public Level Level
        {
            get { return _ContainerLevel; }
            set { _ContainerLevel = value; }
        }
    }
}